import axios from 'axios';
import qs from 'qs'
const config = require('../../config');

let url = "";
// 创建一个新的axios实例
const instance = axios.create({
    baseURL: url,
    headers: {
		"content-type": "application/json; charset=utf-8"
	}
})

// 请求拦截器
instance.interceptors.request.use((config) => {
    // 加载loading
    return config;
}, (error) => {
    return Promise.reject(error)
})

// 响应拦截器
instance.interceptors.response.use((config) => {
    // 关闭loading
    return config;
}, (error) => {
    return Promise.reject(error)
})

// 公用http方法
const Http = {
    ajax({
        url = "",
        methods = 'get',
        params = {},
    }){
        return new Promise((resolve, reject) => {
            instance({methods, url, params}).then((res) => {
                resolve(res.data)
            },(error) => {
                alert('系统繁忙，请稍候再试')
                reject(err)
            }).catch(err => {
                console.log(err)
            })
        })
    },

    // 公用get方法
    get({url = "", params = {}}){
        return this.ajax({
            method: 'get',
            url, 
            params
        })
    },

    // 公用post方法
    post({
        url = '',
        params = {},
    }) {
        return this.ajax({
            method: 'post',
            url,
            params,
        })
    }
}

export default Http;
